import QtQuick 1.0



Rectangle {
    id: sky
    width: 800
    height: 480
    gradient: Gradient {
        GradientStop { position: 0.0; color: "DeepSkyBlue" }
        GradientStop { position: 1.0; color: "LightSkyBlue" }
    }

    state: "Inicio"

    MouseArea {
        x: 0
        y: 0
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.leftMargin: 0
        anchors.topMargin: 0
        anchors.fill: parent
        onClicked: {
            if (sky.state == "Inicio") {
                sky.state = "Mira"
                cannon_move.complete();
            } else if (sky.state == "Mira") {
                sky.state = "Potencia"
                 power_move.complete();
            } else if (sky.state == "Potencia") {
                sky.state = "Atirando"
            } else if (sky.state == "Atirando") {
                sky.state = "Resultado"
            } else if (sky.state == "Resultado") {

                sky.state = "Inicio"
            }
        }
    }

    Rectangle{
        id: grass
        x: 0
        y: 360
        width: 800
        height: 120
        gradient: Gradient {
            GradientStop { position: 0.0; color: "ForestGreen" }
            GradientStop { position: 1.0; color: "DarkGreen" }
        }
    }

    Rectangle{
        id: cannon
        x: cannon_base.x+3*cannon_base.width/4
        y: cannon_base.y+cannon_base.height/2 - cannon_base.height*(270-cannon_angle.angle)/360
        width: cannon_base.width/2
        height: 2*cannon_base.width
        transform: cannon_angle
        color: "#000000"
        border.color: "#000000"
        Rotation {
            id: cannon_angle
            angle: 180   //O angulo deve variar entre 180 e 270 graus
            property int ang: 180
        }
        SequentialAnimation {
            id: cannon_move
            loops: Animation.Infinite
            running: false
            NumberAnimation { target: cannon_angle; property: "angle"; to: 270; duration: 3000 }
            NumberAnimation { target: cannon_angle; property: "angle"; to: 180; duration: 3000 }
        }
    }

    Rectangle {
        id: cannon_base
        x: 35
        y: grass.y - width
        width: 20
        height: 20
        color: "gray"
        border.color: "black"
        Text {
            x:50
            y:30
            text: 270 - cannon_angle.angle
        }
        Text {
            x:0
            y:30
            text: "Angle:"
        }
    }

    Item {
        id: stage
        property int stg:1
        property int level: 1
        Text {
            x: 500
            y:10
            text: "Stage:"
        }
        Text {
            x: 550
            y:10
            text: stage.stg
        }
        Text {
            x: 500
            y:30
            text: "Level:"
        }
        Text {
            x: 550
            y: 30
            text: stage.level
        }
    }


    Rectangle {
        id: block
        height: 35*stage.stg*stage.stg
        width: 60
        x: 350
        y: grass.y - height
        color: "grey"
        border.color: "black"
    }


    Item{
        id: target
        property int g:1
        property int n_a : 270 - cannon_angle.angle
        property int vx: (power.x * (90 - n_a ) /90)/2
        property int vy: (power.x * n_a / 90)/2
        property int t:  vy/g
        property int real_time: 100*t
        x: t*vx
        y: vy*t - g*t*t/2
    }


    Text {
        x: 650
        y: 30
        text: "Distance: "
    }

    Text {
        x: 700
        y: 30
        text: opponent.x - cannon_ammo.x
    }

    Item {
        id: opponent
        x: 700
        y: 180
        Rectangle {
            id: opponent_rect
            width:20
            height:20
            y: opponent.y - opponent_rect.width
            color: "red"
            border.color: "black"
        }
    }

    Rectangle {
        id: cannon_ammo
        width: cannon_base.width/2
        height: cannon_base.width/2
        radius: cannon_base.width/2
        x: cannon_base.x
        y: cannon_base.y
        color: "gray"
        ParallelAnimation {
            id: ammo_move
            running: false
            NumberAnimation { target: cannon_ammo; property: "x"; to: cannon_ammo.x+target.x; duration: target.real_time }
            SequentialAnimation {
                running: false
                NumberAnimation { target: cannon_ammo; property: "y"; easing.type: Easing.OutQuad; to: grass.y-target.y; duration: target.real_time/2 }
                NumberAnimation { target: cannon_ammo; property: "y"; easing.type: Easing.InQuad; to: grass.y; duration: target.real_time/2 }
            }
        }

        onXChanged: {
            if( cannon_ammo.x > block.x && cannon_ammo.x < block.x + block.width && cannon_ammo.y > block.y ) {
                ammo_move.stop();
            } else if (cannon_ammo.x > opponent.x && cannon_ammo.x < opponent.x + opponent.width && cannon_ammo.y > opponent.y) {
                ammo_move.stop();
            }
        }
    }

    Image {
        source: "../../../cannons/resources/power.png"
        Text {
            text: "Min"
            anchors.leftMargin: 0
        }
        Text {
            text: "Max"
            x: 120
        }
    }

    Text {
        id: action
        text: "Clique para começar"
        x: 400
        visible: false
    }

    Rectangle{
        id: power
        x: 0
        y: 0
        width: 3
        height: 35
        color: "#000000"
        border.color: "#000000"
        SequentialAnimation {
            id: power_move
            loops: Animation.Infinite
            running: false
            NumberAnimation { target: power; property: "x"; to: 150-power.width; duration: 3000/stage.level }
            NumberAnimation { target: power; property: "x"; to: 0; duration: 3000/stage.level }
        }
    }

    Text {
        x:20
        y:50
        text: "Power: "
    }
    Text {
        x:70
        y:50
        text: power.x
    }

    //-----SISTEMA DE ESTADOS-----
    states: [
        State {
             name: "Inicio"

             PropertyChanges {
                 target: cannon_ammo
                 x: cannon_base.x
             }
             PropertyChanges {
                 target: cannon_ammo
                 y: cannon_base.y
             }
             PropertyChanges {
                 target: action
                 visible: true
             }
             PropertyChanges {
                 target: action
                 text: "Clique para começar"
             }
             PropertyChanges {
                 target: stage
                 explicit: true
                 stg: stage.stg
                 level: stage.level
             }
        },
        State {
            name: "Mira"

            PropertyChanges {
                target: action
                visible: false
            }
            PropertyChanges {
                target: cannon_move
                running: true
            }
            PropertyChanges {
                target: stage
                explicit: true
                stg: stage.stg
                level: stage.level
            }
        },
        State {
            name: "Potencia"

            PropertyChanges {
                target: power_move
                running: true
            }
            PropertyChanges {
                target: stage
                explicit: true
                stg: stage.stg
                level: stage.level
            }
        },
        State {
             name: "Atirando"
             PropertyChanges {
                 target: action
                 visible: true
             }
             PropertyChanges {
                 target: cannon_ammo
                 explicit: true
                 x: cannon.x - (cannon.width/2)*(270-cannon_angle.angle)/90
                 y: cannon.y - 20*(cannon_angle.angle-180)/90;
             }
             PropertyChanges {
                 target: ammo_move
                 running: true
             }
             PropertyChanges {
                 target: stage
                 explicit: true
                 stg: stage.stg
                 level: stage.level
             }

        },
        State {
             name: "Resultado"
             PropertyChanges {
                 target: action
                 visible: true
             }
             PropertyChanges {
                 target: action
                 text: "Clique para recomeçar"
             }
             PropertyChanges {
                 target: stage
                 explicit: true
                 stg: stage.stg
                 level: stage.level
             }
             StateChangeScript {
                 script:  {
                     if ( (cannon_ammo.x - opponent.x) < 30 && (cannon_ammo.x - opponent.x) > -30 ) {
                         stage.level = stage.level+1;
                         if ( stage.level > 5) {
                             stage.level = 1
                             stage.stg = stage.stg +1
                         }
                     }
                 }
             }
        }
    ]
}
